﻿#define _CRT_SECURE_NO_WARNINGS
#include"Tree.h"
#include"Queue.h"

//申请树节点
BTNode* buyNode(char x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->left = NULL;
	newnode->right = NULL;
	return newnode;
}

//前序遍历-根左右-递归
void PreOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	printf("%c ", root->data);//根
	PreOrder(root->left);//左
	PreOrder(root->right);//右
}

//中序遍历-左根右
void InOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	InOrder(root->left);//左
	printf("%c ", root->data);//根
	InOrder(root->right);//右
}

//后序遍历-左右根
void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	PostOrder(root->left);//左
	PostOrder(root->right);//右
	printf("%c ", root->data);//根
}

//过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
{
	if (*pi >= n || a[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}
	BTNode* root = buyNode(a[*pi]);
	(*pi)++;

	root->left = BinaryTreeCreate(a, n, pi);
	root->right = BinaryTreeCreate(a, n, pi);

	return root;
}

//二叉树节点个数 = 根节点（1） + 左子树节点个数 + 右子树节点个数
int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}

//二叉树叶子节点个数 = 左子树叶子节点个数 + 右子树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}
	return BinaryTreeLeafSize(root->right) + BinaryTreeLeafSize(root->left);
}

//二叉树第k层结点的个数 = 左子树第k层节点数 + 右子树第k层节点数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (k != 1)
	{
		return BinaryTreeLevelKSize(root->left, k-1) + BinaryTreeLevelKSize(root->right, k-1);
	}
	else if(k == 1 && root != NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//二叉树的深度 = 根节点 + max{左子树的深度,右子树的深度}
int BinaryTreeDepth(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}

	int leftDepth = BinaryTreeDepth(root->left);
	int rightDepth = BinaryTreeDepth(root->right);
	return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth);
}

//二叉树查找值为x的节点-左有出左，右有出右
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
	{
		return NULL;
	}
	if (root->data == x)
	{
		return root;
	}
	BTNode* leftFind = BinaryTreeFind(root->left, x);
	if (leftFind)
	{
		return leftFind;
	}
	BTNode* rightFind = BinaryTreeFind(root->right, x);
	if (rightFind)
	{
		return rightFind;
	}
	return NULL;
}

//二叉树销毁-左子树销毁->右子树销毁->销毁根节点
void BinaryTreeDestroy(BTNode** proot)//传二级指针，链表头节点的地址会改变
{
	if (*proot == NULL)
	{
		return;
	}
	BinaryTreeDestroy(&((*proot)->left));
	BinaryTreeDestroy(&((*proot)->right));
	free(*proot);
	*proot = NULL;//把指针变量本身改掉（置空、指向新对象、删除并返回NULL）->传指针的地址
}

//层序遍历->广度优先遍历->借助队列
void LevelOrder(BTNode* root)
{
	Queue q;//建队
	QueueInit(&q);

	//根节点入队
	//循环判断队列是否为空，非空则取队头，队头节点不为空的孩子入队，队头出队
	//为空则跳出循环
	QueuePush(&q, root);
	while (!QueueEmpty(&q))
	{
		BTNode* top = QueueFront(&q);
		printf("%c ", top->data);
		QueuePop(&q);

		if (top->left)
		{
			QueuePush(&q, top->left);
		}
		if (top->right)
		{
			QueuePush(&q, top->right);
		}
	}

	QueueDestroy(&q);
}

//判断⼆叉树是否是完全⼆叉树
bool BinaryTreeComplete(BTNode* root)
{
	Queue q;//建队
	QueueInit(&q);

	//根节点入队
	//循环判断队列是否为空，非空则取队头，队头节点不为空的孩子入队，队头出队
	//队头节点为空则跳出循环-判断剩余队列是否为空
	QueuePush(&q, root);
	while (!QueueEmpty(&q))
	{
		BTNode* top = QueueFront(&q);
		QueuePop(&q);

		if (top == NULL)
		{
			break;
		}

		QueuePush(&q, top->left);
		QueuePush(&q, top->right);
	}
	while (!QueueEmpty(&q))
	{
		BTNode* top = QueueFront(&q);
		QueuePop(&q);
		if (top != NULL)
		{
			//不是完全二叉树
			QueueDestroy(&q);
			return false;
		}
	}
	QueueDestroy(&q);
	return true;
}